package com.back.Controller;

import com.back.Dao.R;
import com.back.Dao.User;
import com.back.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public R findAllDeice()
    {
        List<User> users = userService.findAllUser();
        if(users.size() > 0)
            return R.success(users);
        else
            return R.error("未查询到信息");
    }

    @GetMapping("/user/{id}")
    public R findDeviceById(@PathVariable Integer id) {
        User user = userService.findUserById(id);
        if(user != null)
            return R.success(user);
        else
            return R.error("未查询到此id");
    }

    @DeleteMapping("/user/{id}")
    public R DelDevice(@PathVariable int id) {
        Integer result = userService.DelUser(id);
        if(result == 0)
            return R.error("删除失败，请勿重复删除");
        return R.success(result);
    }

    @PostMapping("/user")
    public R addDevice(@RequestBody User user) {
        if(user.getUserPwd().equals("") || user.getUserName().equals("") || user.getRealName().equals("") || user.getTel().equals("") || user.getEmail().equals(""))
            return R.error("值为空");
        userService.addUser(user);
        return R.success("插入成功");
    }

    @PostMapping("/updateuser")
    public R updateDevice(@RequestBody User user) {
        userService.updateUser(user);
        return R.success("修改成功");
    }

}
